package com.yycx.module.bus.provider.handler.common;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.enums.DeptTypeEnum;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusSalemanBalanceService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import com.yycx.module.user.client.entity.AppPosition;
import com.yycx.module.user.provider.service.AppPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.MessageFormat;

/**
 * 两票业务员保存或者更新处理器
 *
 * @author zyf
 */
@Component
public class BusSaleManSaveHandler implements SaveInterceptor<BusSaleman> {

    @Resource
    private SysCompanyService companyService;

    @Autowired
    @Lazy
    private BusSalemanService salemanService;

    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusSaleman saleMan) {
        Long companyId = OpenHelper.getCompanyId();
        Long deptId = saleMan.getDeptId();
        SysCompany company = companyService.getById(companyId);
        Long organizationId = company.getOrganizationId();
        CriteriaQuery cq = new CriteriaQuery(BusSaleman.class);
        Long n = salemanService.count(cq.eq(true, "saleManName", saleMan.getSaleManName()).eq(true, "companyId", companyId).eq(true, "deptId", deptId).eq(true, "organizationId", organizationId));
        ApiAssert.lezero("业务员已存在", n.intValue());
        saleMan.setSalemanState(1);
        saleMan.setCompanyId(companyId);
        saleMan.setOrganizationId(organizationId);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusSaleman saleMan) {

    }


    @Override
    public void prepare(CriteriaUpdate cu, EntityMap entityMap, BusSaleman saleMan) {

    }

    @Override
    public void complete(CriteriaUpdate cu, EntityMap params, BusSaleman saleMan) {

    }

}
